<template>
  <el-dialog :visible.sync="dialogVisible" width="744px" title="邮件待办设置" center
             :close-on-click-modal="false" :close-on-press-escape	='false'>
    <div class="show-radio">
      <el-radio-group v-model="radio">
        <el-radio :label="1">今天（{{nowTime}}）</el-radio>
        <el-radio :label="2">明天（08:00）</el-radio>
        <el-radio :label="3">后天（08:00）</el-radio>
        <el-radio :label="4">下周{{nextWeek}}（08:00）</el-radio>
        <el-radio :label="5" style="width: 60%;max-width: 500px">
          自定义
          <el-date-picker size="mini" v-model="nextTime" align="center" value-format="yyyy-MM-dd HH:mm:ss" @change="choose"
                          type="datetime" :clearable="false" placeholder="请选择日期时间"  :picker-options="pickerOptions">
          </el-date-picker>
        </el-radio>
      </el-radio-group>
    </div>
    <!-- 底部按钮-->
    <div slot="footer" class="dialog-footer" align="right">
      <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
      <el-button type="primary" @click="submit" size="mini" :loading="loading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {getHour, getNextDate,getDate,getNext} from "@/util/util"
    import {nextEmail} from "@/api/mail/make"

    export default {
        name: "TimeDialog",
        data() {
            return {
                dialogVisible: false,
                radio: 1,
                nowTime: '',
                nextTime: '',
                nextWeek: '',
                tEmailInfoIds: [],
                pickerOptions: {
                    disabledDate: time => {
                        return time.getTime() < Date.now() - 8.64e7
                    }

                },
                loading:false
            }
        },
        methods: {
            choose(){
              this.radio=5
            },
            submit() {
                const time = this.getTime();
                if(!time||time.indexOf('NaN')!=-1){//，没找到
                    this.$message({
                        type: "success",
                        message: '请选择自定义时间'
                    });
                    return;
                }
                this.loading=true;
                var endTime=time;
                if(time.substring(11,13)=='24'){
                    endTime = time.substring(0,10)+" 23:59:59"
                }
                nextEmail({
                    processTime: endTime,
                    temailInfoIds: this.tEmailInfoIds
                }).then(res => {
                    this.loading=false;
                    this.$message({
                        type: "success",
                        message: res.data.msg
                    });
                    this.bus.$emit('refreshEmailNum')
                    this.$emit('getData')
                    this.dialogVisible = false;
                }).catch(err => {
                    this.loading=false;
                });
            },
            getTime() {
                switch (this.radio) {
                    case 1:
                        return getDate(0)+" "+this.nowTime+":00";
                    case 2:
                        return getDate(1)+" 08:00:00";
                    case 3:
                        return getDate(2)+" 08:00:00";
                    case 4://下周
                        let result;
                        if(this.nextWeek=='一'){
                            result = getNext(1)
                        }else if(this.nextWeek=='二'){
                            result= getNext(2)
                        }else if(this.nextWeek=='三'){
                            result=getNext(3)
                        }
                        return result+" 08:00:00";
                    case 5:
                        return this.nextTime;
                    default:
                        return
                }
            },
            init(rowID,processTime) {
                this.tEmailInfoIds = [];
                this.tEmailInfoIds.push(rowID)
                this.radio = 1;
                this.nextWeek = getNextDate();
                this.nowTime = getHour();
                this.nextTime='';
                this.dialogVisible = true;
            }
        }
    }
</script>

<style scoped lang="scss">
  .el-radio-group {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    width: 80%;
    margin: 33px auto 0 auto;

    .el-radio {
      width: 30%;
      margin-bottom: 30px;
    }
  }
</style>
